;(function(M,window){
	M.ui.define('customDialog',function(){
		function customDialog(ops,args){
			this.ops=ops;
			this.args=args;
			this.init();
		};
		customDialog.prototype={
			constructor:customDialog,
			init:function(){
				this.base=M.static.init();
				this.exec();
				return this;
			},
			creat:function(fn){
				this.size=40;
				this.innerHeight=M(window).height();
				this.maxHeight=M(document).height();
				this.scrollTop=/absolute/g.test(this.ops.position)?M(window).scrollTop():0;
				this.container=M.renderHTML({
					name:'div',
					proto:{
						'class':'g-master-container g-dialog-master',
						 'id':this.ops.id||'g-dialog-'+M.now(),
						 'style':{
						 	'position':'absolute',
						 	'width':this.setUnit(M(document).width()),
						 	'height':this.setUnit(M(document).height()),
						 	'z-index':M.now()
						 }
					}
				});
				this.content=M.renderHTML({
					name:'div',
					proto:{
						'class':'g-master-content',
						 'style':{
						 	'position':this.ops.position,
						 	"left":'50%',
							"width":this.setUnit(this.ops.width),
							"margin-left":this.setUnit(-0.5*this.ops.width),
							"height":this.setUnit(this.ops.height),
							"top":this.setUnit((this.innerHeight-this.ops.height)*0.5+this.scrollTop),
							"margin-top":this.setUnit(0),
							"box-sizing":"content-box"
						 }
					}
				});
				this.body=this.creatBody();
				this.main=this.creatMain();
//				this.load=this.creatLoad();
				M(document.body).append(this.container)
				M('.g-master-container').append(this.content)
				M('.g-master-content').append(this.body)
				M('.g-master-body').append(this.load)
				M('.g-master-body').append(this.main)
				M('.g-master-body').append(this.creatErrorBox())
				this.error=M('.g-dialog-error')
				M(document).bind('keydown',{context:this},this.ese);
				fn.call(this);
			},
			setUnit:function(value){
				return value+'px';	
			},
			exec:function(){
				M.ui.ajax.init({
					dataType:'text',
					url:this.ops.url,
					data:this.ops.data,
					isload:false,
					type:'get',
					success:function(data){
						M.delay(1,function(){
							if(this.error){
								this.error.hide();
								this.set(this.format(data));
							}else{
								this.creat(function(){
									this.set(this.format(data));
								});	
							};
						},this);
					},
					error:function(msg){
						this._status_=true;
						this.fail(this,msg);
					}
				},this);
			},
			fail:function(that,msg,args){
				if(this.error){
					this.error.html(this.creatError(msg));
					this.error.show();
					M('.g-master-loading').hide();
					this.reload(this);
				}else{
					this.creat(function(){
						this.error.html(this.creatError(msg));
						this.resolve(this,600,350,(this.innerHeight-350)*0.5+this.scrollTop,function(){
							M('.g-master-loading').hide();
							this.error.fadeIn(300,function(){
								that.reload(that);
							});
						});
					});	
				};
			},
			reload:function(that){
				M(that.base._class('_reload')[0],that.body).bind('click',function(){
					that.error.hide();
					M('.g-master-loading').show();
					that.exec();
				})	
			},
			set:function(data){
				M('.g-master-main').html(data);
				var that=this,box=M('.g-master-main').children('div'),top,space=[2,2],
					width=box.outerWidth(true),
					height=box.outerHeight(true)
				if(height>this.innerHeight){
					this.ops.position='absolute';
					M('.g-master-content').css({position:this.ops.position});
				};
				this.resolve(this,width,height,top,function(that){
					M('.g-master-loading').hide();
					box.css({visibility:'visible'}).hide().fadeIn(300,function(){
						M('.g-master-body').css({height:'auto'});
						M('.g-master-content').css({height:'auto'});
//						that.clear();
						M.isFunction(that.ops.callback)&&that.ops.callback(M('.g-master-container'));
					});
					if(that.scroll){
						that.scroll=M.ui.scroll.init({
							scrollbar:{
								className:'dialog',
								style:{
									marginLeft:10,
									marginRight:10,
									marginTop:10,
									marginBottom:10,
									size:8
								}	
							},
							container:M('.g-master-container'),
							mouseWheelSpeed:50
						});
					};
				});		
			},
			resolve:function(that,width,height,top,fn){
				M('.g-master-content').animate({width:width,height:height,marginLeft:-width*0.5,top:top},500,'easeOutBack',function(){
					fn.call(that,that);
				});
				M('.g-master-body').animate({width:width,height:height},500,'easeOutBack');		
				M('.g-master-container').fadeTo(300,1);
			},
			creatMain:function(){
				return M.renderHTML({
					name:'div',
					proto:{
						'class':'g-master-main relative'
					}
				});
			},
			creatBody:function(){
				return M.renderHTML({
					name:'div',
					proto:{
						'class':'g-master-body',
						'style':{
							"width":this.setUnit(this.ops.width),
							"height":this.setUnit(this.ops.height)
						}
					}
				});
			},
			creatLoad:function(){
				return M.renderHTML({
					name:'span',
					proto:{
						'class':'g-master-loading'
					},
					html:'加载中'
				});
			},
			creatErrorBox:function(){
				return M.renderHTML({
					name:'div',
					proto:{
						'class':'g-dialog-error hide'
					}
				})
			},
			creatError:function(msg){
				return M.renderHTML({
						name:'div',
						proto:{
							'class':'g-master-error'
						},
						html:M.renderHTML({
							name:'img',
							proto:{
								'class':'g-master-show',
								"src":M.getNormalPath("dialog_error.png",1,1)	
							}
						})+M.renderHTML({
							name:'p',
							html:(msg||'')+'，'+M.renderHTML({
								name:'a',
								proto:{
									"href":'javascript:;',
									'class':'_reload'
								},
								html:'点击重新加载'
							})
						})
					})
			},
			ese:function(e){
				if(e.keyCode==27){
					e.data.context.dismiss(e);
					return false;
				};
			},
			dismiss:function(e){
				var that=e.data.context;
				that.remove();
				return false;	
			},
			remove:function(){
				var that=this;
				that.container.fadeOut(300,function(){
					that.container.remove();
					if(that.scroll){
						if(that.maxHeight!=that.innerHeight){
							M('html,body').css({height:'auto',overflowY:'visible'});
						};
						that.scroll&&delete that.scroll;
					};
				});
				M(document).unbind('keydown',that.ese);
			},
			format:function(){
				return arguments[0];	
			}
		};
		return {
			defaults:{
				drag:true,
				url:null,
				data:null,
				width:400,
				height:300,
				position:'fixed',
				callback:function(){}
			},	
			init:function(ops){
				return new customDialog(M.extend(true,{},this.defaults,ops),[].slice.call(arguments,1));
			}
		};
	});
})(window.jQuery||window.Zepto||window.xQuery,window);